package com.ti.leevy.util;


import com.ti.leevy.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

/**
 * 连击2次退出程序控制器
 *
 * @author kTian
 *
 */
public class QuitOperateUtil {

	private static int time = 3*1000;	// 连续点击间隔时间
	private static boolean isAgain = false;
	
	@SuppressLint("HandlerLeak")
	private static Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if( isAgain ) {
				isAgain = false;
			}
		}
		
	};
	
	public static void quitProgram( Activity activity ) {
		if( !isAgain ) {
			isAgain = true;
			Toast.makeText(activity, activity.getString(R.string.quit_warn), Toast.LENGTH_SHORT).show();
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						Thread.sleep(time);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					mHandler.sendEmptyMessage(0);
				}
			}).start();
		}else{
			activity.finish();
			// 关闭子线程
			android.os.Process.killProcess(android.os.Process.myPid());
		}
	}
	
}
